<?php use model\adminModel;
    use Controller\Controller;

    class adminController extends Controller{
        private $ADMIN;
        private $ADMIN_SESSION;

        public function __construct() {
            session_start();
            $this->ADMIN = new adminModel();
            $this->ADMIN_SESSION = (isset($_SESSION['admin'])) ? $this->ADMIN_SESSION = $_SESSION['admin'] : false;
        }

        public function indexAction() {
            $this->errorJsonDie(404);
        }

        public function getAction($params = false) {
            $response = [];
            if ($this->request()["method"] !== "GET") {
                $this->errorJsonDie(400);
            }
            if (!isset($_SESSION['admin'])){
                $this->errorJsonDie(403, "unauthorized");
            }
            if (!$params) {
                $response = $this->ADMIN->getAllAdmin();
            } else {
                $response = $this->ADMIN->getAdminBy($params);
            }
            if ($response["status"] == 0) {
                $this->errorJsonDie(403, "Admin not found");
            }
            $this->successJson($response["response"]);
        }

        public function registrationAction() {
            $request = $this->request();

            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request["json"]["phone"]) || !isset($request["json"]["password"]) || !isset($request["json"]["confirm_password"])){
                $this->errorJsonDie(400);
            }

            $phone = $request["json"]["phone"];
            $password = $request["json"]["password"];
            $confirmPassword = $request["json"]["confirm_password"];

            if (strlen($password) <= 5 || strlen($password) >= 13) {
                $this->errorJsonDie(403, "Bad password length");
            }
            if ((string) $password !== (string) $confirmPassword) {
                $this->errorJsonDie(403, "Password mismatch");
            }


            $response = $this->ADMIN->addAdmin($phone, $password);
            if ($response["status"] == 0) {
                $this->errorJsonDie(403, "Exeption");
            }

            $_SESSION["admin"] = [
                "id" => $response["response"]["id"],
                "permission" => $response["response"]["permission"]
            ];
            $this->successJson($response["response"]);
        }

        public function authAction() {
            $request = $this->request();

            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request["json"]["phone"]) || !isset($request["json"]["password"])){
                $this->errorJsonDie(400, "phone & password is required");
            }

            $phone = $request["json"]["phone"];
            $password = $request["json"]["password"];

            $response = $this->ADMIN->adminLogin($phone, $password);
            if ($response["status"] == 0) {
                return $this->errorJsonDie(403, "Exception");
            } else {
                $_SESSION["admin"] = [
                    "id" => $response["response"]["id"],
                    "permission" => $response["response"]["permission"]
                ];
                $this->successJson($_SESSION["admin"]);
            }
        }

        public function exitAction() {
            $request = $this->request();
            if ($request["method"] !== "GET") {
                $this->errorJsonDie(404);
            }
            $_SESSION = array();
        }
    }